﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Data;
using CRMEntity;
using System.Web.Configuration;
using CRMBiz;

namespace CRMBiz
{
    public static class VedaService
    {
        public static Stream processCreditRequest(List<List<string>> DynamicParamter, int flagByCreditCheck, string Company)
        {
            Stream responseStream;
            try
            {
                CRMEntity.Configuration.WebServiceConfiguration webServiceConfiguration = new CRMEntity.Configuration.WebServiceConfiguration();
                if (flagByCreditCheck == 0)
                    webServiceConfiguration = Configuration.WebServiceConfiguration.WebServiceSetParamter("VEDA Credit Service By Client", DynamicParamter, Company);
                else
                    webServiceConfiguration = Configuration.WebServiceConfiguration.WebServiceSetParamter("VEDA Credit Service By VedaFileNo", DynamicParamter, Company);

                string soap = "";
                soap = webServiceConfiguration.ServiceInputXML;
                soap = soap.Replace("\"\"", "\"");

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(webServiceConfiguration.ServiceURL);
                req.ContentType = "text/xml";
                req.Method = "POST";
                using (Stream stm = req.GetRequestStream())
                {
                    using (StreamWriter stmw = new StreamWriter(stm))
                    {
                        stmw.Write(soap);
                    }
                }
                WebResponse response = req.GetResponse();
                responseStream = response.GetResponseStream();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return responseStream;
        }
        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

        public static DataSet ConvertXMLtoDataSet(string response)
        {
            DataSet ds = new DataSet();
            try
            {
                using (StringReader stringReader = new StringReader(response))
                {
                    ds = new DataSet();
                    ds.ReadXml(stringReader);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        public static VedaResponse VedaResponseXMLParser(DataSet ResponseDS)
        {
            CRMEntity.VedaResponse vedaResp = new CRMEntity.VedaResponse();
            try
            {
                DataTable summaryTable = ResponseDS.Tables["summary"];
                if (ResponseDS.Tables["summary"] != null && ResponseDS.Tables["summary"].Rows.Count > 0)
                {
                    if (ResponseDS.Tables["BCAservice"] != null && ResponseDS.Tables["BCAservice"].Rows.Count > 0)
                        if (ResponseDS.Tables["BCAservice"].Columns.Contains("BCAservice-client-ref"))
                            vedaResp.prim_individual_cross_reference_client_reference = ResponseDS.Tables["BCAservice"].Rows[0]["BCAservice-client-ref"].ToString();
                    if (ResponseDS.Tables["primary-match"] != null && ResponseDS.Tables["primary-match"].Rows.Count > 0)
                        if (ResponseDS.Tables["primary-match"].Columns.Contains("bureau-reference"))
                            vedaResp.prim_individual_cross_reference_bureau_reference = ResponseDS.Tables["primary-match"].Rows[0]["bureau-reference"].ToString();
                    foreach (DataRow dr in summaryTable.Rows)
                    {
                        if (dr.ItemArray[1].ToString() == "known-identities")
                            vedaResp.known_identities = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "adverse-on-file")
                            vedaResp.adverse_on_file = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "credit-enquiries-12")
                            vedaResp.credit_enquiries_12 = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "credit-enquiries-60")
                            vedaResp.credit_enquiries_60 = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "total-value-of-outstanding-defaults")
                            vedaResp.total_value_of_outstanding_defaults = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "authorised-agents-enquiries-12")
                            vedaResp.authorised_agents_enquiries_12 = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "age-of-file")
                            vedaResp.age_of_file = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "directorships-current")
                            vedaResp.directorships_current = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "directorships-previous")
                            vedaResp.directorships_previous = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "proprietorships")
                            vedaResp.proprietorships = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "file-notes")
                            vedaResp.file_notes = dr.ItemArray[3].ToString();
                        if (dr.ItemArray[1].ToString() == "authorised-agents-enquiries-60")
                            vedaResp.authorised_agents_enquiries_60 = dr.ItemArray[3].ToString();
                    }
                    if (ResponseDS.Tables["score"] != null && ResponseDS.Tables["score"].Rows.Count > 0)
                    {
                        if (ResponseDS.Tables["score"].Columns.Contains("veda-score"))
                            vedaResp.veda_score = ResponseDS.Tables["score"].Rows[0]["veda-score"].ToString();
                        if (ResponseDS.Tables["score"].Columns.Contains("relative-risk"))
                            vedaResp.relative_risk = ResponseDS.Tables["score"].Rows[0]["relative-risk"].ToString();
                        if (ResponseDS.Tables["score"].Columns.Contains("applicant-odds"))
                            vedaResp.applicant_odds = ResponseDS.Tables["score"].Rows[0]["applicant-odds"].ToString();
                    }
                    if (ResponseDS.Tables["population"] != null && ResponseDS.Tables["population"].Rows.Count > 0)
                        if (ResponseDS.Tables["population"].Columns.Contains("population-odds"))
                            vedaResp.population_odds = ResponseDS.Tables["population"].Rows[0]["population-odds"].ToString();
                    if (ResponseDS.Tables["contributing-factor"] != null && ResponseDS.Tables["contributing-factor"].Rows.Count > 0)
                    {
                        vedaResp.contributing_factor_filed = new List<string>();
                        foreach (DataRow dr in ResponseDS.Tables["contributing-factor"].Rows)
                        {
                            if (ResponseDS.Tables["contributing-factor"].Columns.Contains("impact"))
                                vedaResp.contributing_factor_filed.Add(dr["impact"].ToString());
                        }

                        vedaResp.contributing_factor_value = new List<string>();
                        foreach (DataRow dr in ResponseDS.Tables["contributing-factor"].Rows)
                        {
                            if (ResponseDS.Tables["contributing-factor"].Columns.Contains("contributing-factor_Text"))
                                vedaResp.contributing_factor_value.Add(dr["contributing-factor_Text"].ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return vedaResp;
        }
    }
}
